<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link href="${pageContext.request.contextPath}/layui/src/css/layui.css" rel="stylesheet" media="all">
   <title></title>
  </head>
  
  <body>
  <div align="center" style="margin-top: 3%;">
  	<table id="user-list-table" lay-filter="userListTable"></table>
  </div>
  
  	
  </body>
  
  
<script src="${pageContext.request.contextPath}/layui/src/layui.js"></script>

<script type="text/javascript">

	layui.use(['form','table','jquery'],function(){
		var from = layui.form;
		var table = layui.table;
		var $ = layui.jquery;
		
		console.log("userList.jsp");
		
		/* $.ajax({
			   url:'${pageContext.request.contextPath}/userList.action',
			   type:"post",
			   async:false,
			   data:{
				   
			   },
			   dataType:'json',
			   success:function(data){
				   console.log("data"+JSON.stringify(data.userList));
				   renderTable(data.userList);
			   }
		   })  */ 
		
		//function renderTable(data){
			table.render({
				elem:'#user-list-table',
				url:'${pageContext.request.contextPath}/userList.action',
				width:1460,
				//data:data,
				cols: [[
				   	   {
					   		checkbox: true
					   },{
						   title: '账号',
						   width: 200,
						   align:'center',
						   templet: function(d){
						        return d.username;
						   }
					   },{
						   title: '密码',
						   width: 200,
						   align:'center',
						   templet: function(d){
						        return '<input type="text" style="text-align: center;" id="password_'+d.id+'" value="'+d.password+'" lay-verify="required" placeholder="用户名" class="layui-input">';
						   }
						},{
						   title: '创建时间',
						   width: 200,
						   align:'center',
						   templet: function(d){
						        return d.createTime;
						   }
						},{
						   title: '修改时间',
						   width: 200,
						   align:'center',
						   templet: function(d){
						        return d.updateTime;
						   }
						},{
						   title: '手机号',
						   width: 200,
						   align:'center',
						   templet: function(d){
							   var o = {
							   	   	"id":d.id,
							   		"username":d.username,
							   		"password":d.password,
							   		"phone":d.phone,
							   };
							   var str='<input type="text" style="text-align: center;" id="phone_'+d.id+'" value="'+d.phone+'" lay-verify="required" placeholder="手机号" class="layui-input">';
						       return str;
						   }
						},{
						   title: '状态',
						   width: 200,
						   align:'center',
						   templet: function(d){
							   var str="";
	                            if(d.status==1){
	                                str="<input type=\"checkbox\" value='"+d.id+"' id='orderSyncRuleSwitch"+d.id+"' name=\"orderSyncRuleSwitch\" lay-skin=\"switch\" lay-filter=\"orderSyncRuleSwitch\" lay-text=\"激活|冻结\" checked>";
	                            }else if(d.status==0){
	                                str="<input type=\"checkbox\" value='"+d.id+"' id='orderSyncRuleSwitch"+d.id+"' name=\"orderSyncRuleSwitch\" lay-skin=\"switch\" lay-filter=\"orderSyncRuleSwitch\" lay-text=\"激活|冻结\">";
	                            }
	                            return str;
						   }
						},{
						   title: '操作',
						   width: 200,
						   align:'center',
						   templet: function(d){
							   var o = {
								   	   	"id":d.id,
								   		"username":d.username,
								   		"password":d.password,
								   		"phone":d.phone,
							   };
							   var str = '<button id="userPhone'+d.id+'" class="layui-btn layui-btn-xs" onclick="phoneDetail('+d.id+')">查看</button>'+
							   "<button class='layui-btn layui-btn-xs' onclick='editUser("+d.LAY_INDEX+","+d.id+");'>修改</button>"+
							   '<button class="layui-btn layui-btn-xs" onclick="removeUser('+d.id+');">删除</button>';
						       return str;
						   }
						}
				  ]],response:{
					  dataName:'userList',
					  countName:'count'
				  },done:function (){
	                    $('.layui-table-view .layui-table[lay-size=sm] .layui-table-cell').css({'height': '30px', 'line-height': '30px'});
	                    $('.layui-table-cell').css({'height': 'inherit'});
	                    $('.layui-table-page').css({'height':'40px'});
	              },
				  id:'userListTable',
				  page:true,
				  even:true,
				  size:'sm'
			});
		//} 
		
	});
	
	function phoneDetail(data){
		console.log("phoneDetail="+data);
		layui.use(['form','table','jquery'],function(){
			var $ = layui.jquery;
			var id = data;
			$.ajax({
				url:"${pageContext.request.contextPath}/phoneDetail.action",
				type:'post',
				data:{
					"id":id,
				},
				dataType:'json',
				async:true,
				success:function(data){
					console.log("phoneDetail="+JSON.stringify(data.phone));
					var d = data.phone;
					var str = "省份："+d.province+"<br/>城市："+d.city+"<br/>卡类型："+d.cardType+"";
					layer.msg(str, {
						  icon: 1,
						  time: 2000 //2秒关闭（如果不配置，默认是3秒）
						}, function(){
							
					});  
				},
			})
		});
	}
	
	function editUser(index,data){
		console.log("editUser="+data);
		layui.use(['form','table','jquery'],function(){
			var $ = layui.jquery;
			
			var id = data;
			var password = $("#password_"+id).val();
			var phone = $("#phone_"+id).val();
			if(password==null||password==""){
				layer.msg("第"+(index+1)+"行的密码不能为空 ！");
				return false;
			}
			$.ajax({
				url:"${pageContext.request.contextPath}/doUpdate.action",
				type:'post',
				data:{
					"id":id,
					"password":password,
					"phoneNO":phone,
				},
				dataType:'json',
				async:true,
				success:function(data){
					layer.msg(data);
				},
			})
		});
		
	}
	
	function removeUser(data){
		console.log("removeUser="+data);
		layui.use(['form','table','jquery'],function(){
			var $ = layui.jquery;
			var id = data;
			$.ajax({
				url:"${pageContext.request.contextPath}/doRemove.action",
				type:'post',
				data:{
					"id":id,
				},
				dataType:'json',
				async:true,
				success:function(data){
					//window.location.href="${pageContext.request.contextPath}/userList.action";
					console.log("remove返回");
					//window.location.reload();
				},
			})
		});
		
	}

</script>

</html>
